package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Transactional
@Slf4j
@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    ApplicationMapper applicationMapper;
    @Autowired
    AuditService auditService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AuditMapper auditMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
        log.debug("查询申请单业务:applicationQuery={}",applicationQuery);
        List<ApplicationVO> list = applicationMapper.selectApplication(applicationQuery);
        //遍历取出每一个申请单VO,为其补全审批人数据
        for(int i=0;i<list.size();i++){
            ApplicationVO applicationVO = list.get(i);
            assignAuditUserList(applicationVO);//调用自定义方法
        }
        return list;
    }
    /* 自定义方法：为当前传入的申请单VO对象补全审批人信息，此方法并不会产生额外的申请单VO! */
    private void assignAuditUserList(ApplicationVO applicationVO){
        //1.准备空集合，用来保存查到的审批人id与姓名
        List<Long> auditUserIdList = new ArrayList<>();
        List<String> auditUsernameList = new ArrayList<>();
        //2.获取当前申请单id，查出次申请单对应的所有审批单
        List<AuditVO> auditVOList = auditService.selectAuditByApplicationId(applicationVO.getId());
        //3.依次遍历取车每个审批单VO
        for (int i=0;i<auditVOList.size();i++){
            AuditVO auditVO = auditVOList.get(i);
            //4.获取当前审批单的审批人id
            Long id = auditVO.getAuditUserId();
            auditUserIdList.add(id);
            //5.根据审批人id，查询审批人姓名
            UserVO userVO = userMapper.selectById(id);
            auditUsernameList.add(userVO.getUsername());
        }
        //6.准备一个拼接工具
        StringJoiner stringJoiner = new StringJoiner(",");
        //7.依次将每个审批人姓名用逗号拼接起来
        for(String username : auditUsernameList){
            stringJoiner.add(username);
        }
        //8.将准备好的审批人数组重新设置给申请单VO
        applicationVO.setAuditUserIdList(auditUserIdList);
        applicationVO.setAuditUsernameList(stringJoiner.toString());

    }
    @Override
    public void saveApplication(ApplicationSaveParam applicationSaveParam) {
        log.debug("新增申请单业务：{}", applicationSaveParam);
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam, application);
        application.setStatus(ApplicationStatusEnum.PENGDING.getCode());
        application.setCreateTime(new Date());
        applicationMapper.insert(application);
        /* 还需要为这个申请生成对应的审批数据 */
        auditService.insertAudit(application);
    }

    @Override
    public void cancel(Long id) {
        log.debug("撤销申请单业务：{}", id);
        Application application = new Application();
        application.setId(id);
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        //还需要删除此申请单对应的所有审批单
        auditMapper.deleteByApplicationId(id);
    }

    @Override
    public void distribute(Long applicationId, Long vehicleId) {
        log.debug("分配车辆业务：{},{}", applicationId, vehicleId);
        Application application = new Application();
        application.setId(applicationId);//设置更新那个申请单
        application.setVehicleId(vehicleId);//设置给申请单分配哪辆车
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());//设置申请单状态为“已分配用车"
        application.setUpdateTime(new Date());//设置更新时间
        applicationMapper.update(application);

        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);//设置更新哪辆车
        vehicle.setStatus("2");//设置车辆状态为“占用”
        vehicle.setUpdateTime(new Date());//设置更新时间
        vehicleMapper.update(vehicle);
    }

    @Override
    public void back(Long applicationId, Long vehicleId) {
        log.debug("还车业务：{},{}", applicationId, vehicleId);
        Application application = new Application();
        application.setId(applicationId);
        application.setVehicleId(null);//设置此申请单的车辆id为null
        application.setStatus(ApplicationStatusEnum.END.getCode());
        application.setUpdateTime(new Date());
        //因为之前的update SQL要不为空才拼接条件,我们这里需要将车辆id置为null,所以需要新写SQL
        applicationMapper.back(application);

        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);//设置更新哪辆车
        vehicle.setStatus("1");//设置车辆的状态为"空闲"
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }

}

